package net4.FaWave;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import weibo4android.User;
import weibo4android.Weibo;
import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;

import net4.FaWaveConst;
import net4.PlatformInfo;
import net4.WeiboFactory;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class AccountEditActivity extends BaseActivity {

	private Hashtable<String, PlatformInfo> m_Platforms = null;
	private String[] m_PlatformDisplay = null;  

    private Spinner spPlatform; 
    private TextView txtAuthType;
    private LinearLayout llayoutUserNamePassWord;
    private Button btnLogin;
    private ArrayAdapter<String> adapter;
    private PlatformInfo m_CurrentPlatformInfo = null;
    private Weibo m_Weibo = null;
    private RequestToken m_RequestToken = null;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.account_edit);
        
        InitData();
        BindControls();
    }
    
    private void InitData(){
    	m_Platforms = FaWaveConst.PlatformInfoList;
    	m_PlatformDisplay = new String[m_Platforms.size()];
    	int i = m_Platforms.size() - 1;
    	for(Enumeration<String> iter = m_Platforms.keys(); iter.hasMoreElements(); ){
    		m_CurrentPlatformInfo = m_Platforms.get(iter.nextElement());
    		m_PlatformDisplay[i] = m_CurrentPlatformInfo.getPlatformName();
    		i--;
    	}
    	m_CurrentPlatformInfo = m_Platforms.get(Integer.toString(FaWaveConst.Platform_TSINA));

    }
    
    private void BindControls(){
    	txtAuthType = (TextView)findViewById(R.id.txtAuthType);
    	llayoutUserNamePassWord = (LinearLayout)findViewById(R.id.llayoutUserNamePassWord);
    	
    	spPlatform = (Spinner)findViewById(R.id.spPlatform); 
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_PlatformDisplay);  
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);  
        spPlatform.setAdapter(adapter);  
        spPlatform.setOnItemSelectedListener(new SpinnerSelectedListener());  
        spPlatform.setVisibility(View.VISIBLE);

    	btnLogin  = (Button)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
            	
            }
        });
    }
    
    private void Login(){
//    	switch(m_CurrentPlatformInfo.getPlatform()){
//    	case FaWaveConst.Platform_TSINA:
//    		
//    		break;
//    	case FaWaveConst.Platform_TQQ:
//    		
//    		break;
//    	}
    	if(m_Weibo == null){
    		m_Weibo = WeiboFactory.CreateWeibo(m_CurrentPlatformInfo);
    	}

		try {
			m_RequestToken = m_Weibo.getOAuthRequestToken("myapp://AccountEditActivity");
			Uri uri = Uri.parse(m_RequestToken.getAuthenticationURL()+ "&display=mobile");
			startActivity(new Intent(Intent.ACTION_VIEW, uri));
		} catch (WeiboException e) {
			e.printStackTrace();
		}

    }
    
    @Override
	protected void onNewIntent(Intent intent) {
    	super.onNewIntent(intent);
    	Uri uri = intent.getData();
    	try {
			AccessToken accessToken=m_RequestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));
			m_Weibo.setOAuthAccessToken(accessToken);
			User user = m_Weibo.showUser(String.valueOf(accessToken.getUserId()));
    	} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

    class SpinnerSelectedListener implements OnItemSelectedListener{  

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        	m_CurrentPlatformInfo = m_Platforms.get(Integer.toString(arg2 + 1));
        	txtAuthType.setText(getText(R.string.txtAuthTypeText) + m_CurrentPlatformInfo.getAuthTypeName());
            if(m_CurrentPlatformInfo.getAuthType() != FaWaveConst.AuthType_OAuth){
            	llayoutUserNamePassWord.setVisibility(View.VISIBLE);
            }
        }  

        public void onNothingSelected(AdapterView<?> arg0) {  
        }  
    }
}


